package cn.sumpu.app.guoguojie.biz.impl;

import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sumpu.app.guoguojie.biz.WeiboBiz;
import cn.sumpu.app.guoguojie.dao.DoingDao;
import cn.sumpu.app.guoguojie.domain.DoingSync;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.PageModel;
import cn.sumpu.toolkit.weibo.AccessUser;
import cn.sumpu.toolkit.weibo.SinaWeibo;
import cn.sumpu.toolkit.weibo.TencentWeibo;

public class WeiboBizImpl implements WeiboBiz {

    private String   sinaWeiboAppKey;
    private String   sinaWeiboAppSecret;
    private String   tencentWeiboAppKey;
    private String   tencentWeiboAppSecret;
    private String   webServer;
    private String   imageDir;

    private DoingDao doingDao;

    public void setSinaWeiboAppKey(String sinaWeiboAppKey) {
        this.sinaWeiboAppKey = sinaWeiboAppKey;
    }

    public void setSinaWeiboAppSecret(String sinaWeiboAppSecret) {
        this.sinaWeiboAppSecret = sinaWeiboAppSecret;
    }

    public void setTencentWeiboAppKey(String tencentWeiboAppKey) {
        this.tencentWeiboAppKey = tencentWeiboAppKey;
    }

    public void setTencentWeiboAppSecret(String tencentWeiboAppSecret) {
        this.tencentWeiboAppSecret = tencentWeiboAppSecret;
    }

    public void setWebServer(String webServer) {
        this.webServer = webServer;
    }

    public void setDoingDao(DoingDao doingDao) {
        this.doingDao = doingDao;
    }

    public void setImageDir(String imageDir) {
        this.imageDir = imageDir;
    }

    @Override
    public void sendMessageSina(String text, String accessToken, String accessSecret) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        sinaWeibo.sendMessage(text, accessToken, accessSecret);

    }

    @Override
    public void sendMessageNetPicSina(String text, String picUrl, String accessToken, String accessSecret) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        sinaWeibo.sendMessage(text, accessToken, accessSecret);

    }

    @Override
    public String requestOauthSina() throws AppBusinessException {
        String callbackUrl = webServer + "member/sinacallback.htm";
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        return sinaWeibo.requestOauth(callbackUrl);
    }

    @Override
    public String requestOauthSina(String callbackUrl) throws AppBusinessException {
        callbackUrl = webServer + "member/sinacallback.htm?historyUrl=" + callbackUrl;
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        return sinaWeibo.requestOauth(callbackUrl);
    }

    @Override
    public AccessUser requstAccessTokenSina(String oauthVerifier) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        return sinaWeibo.requstAccessToken(oauthVerifier);
    }

    @Override
    public AccessUser getUserInfoSina(String userId, String accessToken, String accessSecret) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        return sinaWeibo.getUserInfo(userId, accessToken, accessSecret);
    }

    @Override
    public String shareSina(String text, String url, String picUrl, String site) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        return sinaWeibo.share(text, url, picUrl, site, sinaWeiboAppKey);
    }

    @Override
    public String shareUrlSina(String text, String url, String site) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        return sinaWeibo.shareUrl(text, url, site, sinaWeiboAppKey);
    }

    @Override
    public String sharePicSina(String text, String picUrl, String site) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        return sinaWeibo.sharePic(text, picUrl, site, sinaWeiboAppKey);
    }

    @Override
    public String shareTextSina(String text, String site) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        return sinaWeibo.shareText(text, site, sinaWeiboAppKey);
    }

    @Override
    public void sendMessageLocalPicSina(String text, String picUrl, String accessToken, String accessSecret) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        sinaWeibo.sendMessageLocalPic(text, picUrl, accessToken, accessSecret);
    }

    @Override
    public void addFollowSina(String weiboUserId, String accessToken, String accessSecret) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        sinaWeibo.addFollow(weiboUserId, accessToken, accessSecret);
    }

    @Override
    public void delFollowSina(String weiboUserId, String accessToken, String accessSecret) throws AppBusinessException {
        SinaWeibo sinaWeibo = new SinaWeibo(sinaWeiboAppKey, sinaWeiboAppSecret);
        sinaWeibo.delFollow(weiboUserId, accessToken, accessSecret);
    }

    @Override
    public String requestOauthTencent() throws AppBusinessException {
        String callbackUrl = webServer + "member/tencentcallback.htm";
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        return tencentWeibo.requestOauth(callbackUrl);
    }

    @Override
    public String requestOauthTencent(String callbackUrl) throws AppBusinessException {
        callbackUrl = webServer + "member/tencentcallback.htm?historyUrl=" + callbackUrl;
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        return tencentWeibo.requestOauth(callbackUrl);
    }

    @Override
    public AccessUser requstAccessTokenTencent(String oauthVerifier) throws AppBusinessException {
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        return tencentWeibo.requstAccessToken(oauthVerifier);
    }

    @Override
    public AccessUser getUserInfoTencent(String userId, String accessToken, String accessSecret) throws AppBusinessException {
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        return tencentWeibo.getUserInfo(accessToken, accessSecret);
    }

    @Override
    public String shareTencent(String text, String url, String picUrl, String site) throws AppBusinessException {
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        return tencentWeibo.share(text, url, picUrl, site, tencentWeiboAppKey);
    }

    @Override
    public String shareUrlTencent(String text, String url, String site) throws AppBusinessException {
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        return tencentWeibo.shareUrl(text, url, site, tencentWeiboAppKey);
    }

    @Override
    public String sharePicTencent(String text, String picUrl, String site) throws AppBusinessException {
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        return tencentWeibo.sharePic(text, picUrl, site, tencentWeiboAppKey);
    }

    @Override
    public String shareTextTencent(String text, String site) throws AppBusinessException {
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        return tencentWeibo.shareText(text, site, tencentWeiboAppKey);
    }

    @Override
    public void sendMessageTencent(String text, String accessToken, String accessSecret, String clientIP) throws AppBusinessException {
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        tencentWeibo.sendMessage(text, accessToken, accessSecret, clientIP);
    }

    @Override
    public void sendMessageLocalPicTencent(String text, String picUrl, String accessToken, String accessSecret, String clientIP) throws AppBusinessException {
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        tencentWeibo.sendMessageLocalPic(text, picUrl, accessToken, accessSecret, clientIP);
    }

    @Override
    public void addFollowTencent(String weiboUserId, String accessToken, String accessSecret, String clientIP) throws AppBusinessException {
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        tencentWeibo.addFollow(weiboUserId, accessToken, accessSecret, clientIP);
    }

    @Override
    public void delFollowTencent(String weiboUserId, String accessToken, String accessSecret) throws AppBusinessException {
        TencentWeibo tencentWeibo = new TencentWeibo(tencentWeiboAppKey, tencentWeiboAppSecret);
        tencentWeibo.delFollow(weiboUserId, accessToken, accessSecret);
    }

    @Override
    public void saveDoingSync(DoingSync doingSync, String saveType) throws AppBusinessException {
        if (!Common.empty(doingSync)) {
            if (saveType.equals("add") && !doingSync.getIdentifier().equals("")) {
                doingDao.insert(doingSync);
            } else if (saveType.equals("edit") && !doingSync.getIdentifier().equals("")) {
                doingDao.updateDoingSync(doingSync);
            }
        }
    }

    @Override
    public void deleteDoingSync(String doingSyncId) throws AppBusinessException {
        doingDao.deletePostSyncById(doingSyncId);
    }

    @Override
    public DoingSync getDoingSyncById(String doingSyncId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", doingSyncId);
        List<DoingSync> list = doingDao.getDoingSync(map);
        if (list != null && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public PageModel getDoingSync(Integer page, Integer pageSize, String status) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        page = page == null ? 1 : page;
        int size = pageSize == null ? 10 : pageSize;
        map.put("startId", (page - 1) * size);
        map.put("pageSize", size);
        if (!Common.empty(status)) {
            map.put("status", status);
        }
        int count = doingDao.getDoingSyncCount(map);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        List<DoingSync> list = doingDao.getDoingSync(map);
        pm.setList(list);
        pm.setPageSize(size);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + size - 1) / size);
        return pm;
    }

    public void timePost() {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("status", "ready");
            map.put("currentTime", Common.getFormatDate(new Date(), "yyyy-MM-dd HH:mm"));
            List<DoingSync> doingSyncList = doingDao.getDoingSync(map);
            if (doingSyncList != null && doingSyncList.size() > 0) {
                for (DoingSync sync : doingSyncList) {
                    String text = sync.getContent();
                    String picUrl = sync.getPictureUrl();
                    if (!Common.emptyStr(picUrl)) {
                        int beginIndex = picUrl.indexOf("pic");
                        if (beginIndex > 0) {
                            picUrl = imageDir + "/" + picUrl.substring(beginIndex);
                            sendMessageLocalPicSina(URLEncoder.encode(text, "UTF-8"), picUrl, sinaWeiboAppKey, sinaWeiboAppSecret);
                            sendMessageLocalPicTencent(text, picUrl, tencentWeiboAppKey, tencentWeiboAppSecret, sync.getPostIp());
                            sync.setStatus("ok");
                            doingDao.updateDoingSync(sync);
                        }
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        String picUrl = "http://192.168.0.106:8080/pic/weibo/100000009.jpg";
        if (!Common.emptyStr(picUrl)) {
            int beginIndex = picUrl.indexOf("pic");
            if (beginIndex > 0) {
                picUrl = picUrl.substring(beginIndex);
                System.out.println(picUrl);
            }

        }
    }
}
